/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zjdt.epserver.servlet.driver;

import com.google.gson.Gson;
import com.zjdt.epserver.db.service4mysql.FactoryServiceImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 加载所有客户工厂信息供手机端组合框使用，包括潜在客户
 *
 * @author dusin
 */
@WebServlet(name = "LoadAllFactory", urlPatterns = {"/servlet/driver/LoadAllFactory"})
public class LoadAllFactory extends HttpServlet {

    private FactoryServiceImpl instance;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            instance = (FactoryServiceImpl) context.getBean("factoryService");
            FactoryServiceImpl instance2 = (FactoryServiceImpl) context.getBean("factoryPotentialService");
            List<Map<String, Object>> factories = instance.findAll4Combo();
            List<Map<String,Object>> factories2 = instance2.findAll4Combo();
            factories.addAll(factories2);
            Gson gson = new Gson();
            Collections.sort(factories, new Comparator(){
                @Override
                public int compare(Object arg0, Object arg1) {//按工厂名排序
                    Map<String,Object> f1=(Map)arg0;
                    Map<String,Object> f2 = (Map)arg1;
                    String fname1 =(String) f1.get("name");
                    String fname2 = (String)f2.get("name");
                    return fname1.compareTo(fname2);
                }            
            });
            String jsonText = gson.toJson(factories);
            out.print(jsonText);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
